package com.file.system.controller.system;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.file.system.entity.dict.OnOff;
import com.file.system.entity.res.CommonResult;
import com.file.system.entity.res.DataPage;
import com.file.system.entity.system.WebFilterReleasePath;
import com.file.system.service.system.IWebFilterReleasePathService;
import com.file.system.tools.regular.RegularUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

import static cn.hutool.core.text.CharSequenceUtil.isNotEmpty;


/**
 * 放行请求
 *
 * @author jch
 */
@RestController
@Validated
@RequestMapping("/web/filter/release/path")
public class WebFilterReleasePathController {

    @Autowired
    private IWebFilterReleasePathService service;


    /**
     * 查询放行请求分页
     *
     * @param pageIndex 页数
     * @param pageSize  每页条数
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/page/{pageIndex}/{pageSize}")
    public CommonResult<DataPage<List<WebFilterReleasePath>>> select(@PathVariable
                                                                     @Min(value = 1, message = "页数最小1")
                                                                     Integer pageIndex,
                                                                     @PathVariable
                                                                     @Min(value = 1, message = "每页条数最小1")
                                                                     @Max(value = 100, message = "每页条数最大100")
                                                                     Integer pageSize,
                                                                     @RequestBody @Validated WebFilterReleasePath webFilterReleasePath
    ) {
       QueryWrapper<WebFilterReleasePath> query = new QueryWrapper<>();
        // 根据id降序排列
        query.orderByDesc("a.id");
        // 判断是否删除
        query.eq("a.is_del", 0);
        // 判断是否包含releasePath
        query.like(isNotEmpty(webFilterReleasePath.getReleasePath()), "a.name", webFilterReleasePath.getReleasePath());
        // 判断是否包含dictOnOff
        query.like(isNotEmpty(webFilterReleasePath.getDictOnOff()), "a.dict_on_off", webFilterReleasePath.getDictOnOff());

        DataPage<List<WebFilterReleasePath>> dp = service.page(pageIndex, pageSize, query);
        return CommonResult.ok().setResult(dp);
    }


    /**
     * 通过ids查询放行请求集合
     *
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/listByIds")
    public CommonResult<List<WebFilterReleasePath>> listByIds(@RequestBody List<Long> ids) {
        //查询集合
        List<WebFilterReleasePath> list = service.listByIds(ids);
        return CommonResult.ok().setResult(list);
    }

    /**
     * 查询所有非删除状态放行请求
     *
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/listAll")
    public CommonResult<List<WebFilterReleasePath>> listAll() {
        //查询集合
        List<WebFilterReleasePath> list = service.listAll();
        return CommonResult.ok().setResult(list);
    }


    /**
     * 通过id查询放行请求
     *
     * @param id 放行请求id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/id/{id:" + RegularUtil.ID + "}")
    public CommonResult<WebFilterReleasePath> getById(@PathVariable
                                                      @Min(value = 1, message = "id长度最小1")
                                                      Long id) {
        //查询集合
        WebFilterReleasePath webFilterReleasePath = service.getById(id);
        return CommonResult.ok().setResult(webFilterReleasePath);
    }


    /**
     * 通过id删除放行请求
     *
     * @param id id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/remove/{id:" + RegularUtil.ID + "}")
    public CommonResult removeById(@PathVariable
                                   @Min(value = 1, message = "id长度最小1")
                                   Long id) {
        boolean state = service.removeById(id);
        //返回
        return CommonResult.check(state);
    }

    /**
     * 通过ids删除放行请求
     *
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/removeByIds")
    public CommonResult removeById(@RequestBody List<Long> ids) {
        //返回
        return CommonResult.check(service.removeByIds(ids));
    }


    /**
     * 通过id编辑放行请求
     *
     * @param webFilterReleasePath 放行请求
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/updateById")
    public CommonResult<WebFilterReleasePath> updateById(@RequestBody @Validated(WebFilterReleasePath.update.class) WebFilterReleasePath webFilterReleasePath) {
        //编辑修改
        boolean state = service.updateById(webFilterReleasePath);
        return CommonResult.check(state).setResult(webFilterReleasePath);
    }


    /**
     * 添加放行请求
     *
     * @param webFilterReleasePath 放行请求
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/save")
    public CommonResult<WebFilterReleasePath> save(@RequestBody @Validated(WebFilterReleasePath.add.class) WebFilterReleasePath webFilterReleasePath) {
        //添加放行请求
        boolean state = service.save(webFilterReleasePath);
        return CommonResult.check(state).setResult(webFilterReleasePath);
    }

    /**
     * 启用禁用
     *
     * @param id id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/isStart/{id}")
    public CommonResult start(@PathVariable
                              @Min(value = 1, message = "id长度最小1")
                              Long id) {
        WebFilterReleasePath webFilterReleasePath = service.getById(id);
        //如果DictOnOff为ON，则将其设置为OFF
        if (ObjectUtil.equals(OnOff.ON.val, webFilterReleasePath.getDictOnOff())) {
            webFilterReleasePath.setDictOnOff(OnOff.OFF.val);
        } else {
            //否则设置为ON
            webFilterReleasePath.setDictOnOff(OnOff.ON.val);
        }
        //更新放行请求
        boolean state = service.updateById(webFilterReleasePath);
        return CommonResult.check(state);
    }
}

